#CalculatePi.py

import random
import time

def v1CalculPi(num):
    pi = 0.0
    for k in range(num):
        pi += 1/pow(16,k)*(4/(8*k+1) - 2/(8*k+4) - 1/(8*k+5) - 1/(8*k+6))
    print("圆周率值是：{}".format(pi))

def v2CalculPi(darts):
    pi = 0.0
    hits = 0.0
    start = time.perf_counter()
    for i in range(darts):
        x,y = random.random(),random.random()
        dist = pow(x**2 + y**2,0.5)
        if dist <= 1.0:
            hits += 1
    pi = 4*(hits/darts)
    runtime = time.perf_counter() - start
    print("圆周率值是：{}".format(pi))
    print("运行时间是：{:.5f}s".format(runtime))
    
N = 100
v1CalculPi(N)
darts = 1000*1000*1000
v2CalculPi(darts)